<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>


<c:if test="${dataSource==null}">
  <sql:setDataSource var="dataSource" driver="org.gjt.mm.mysql.Driver"
  url="jdbc:mysql://localhost/forum?user=forumuser"
  scope="session" />
</c:if>

<c:if test="${lang==null}">
  <fmt:setBundle basename="com.heaton.bundles.Forum" var="lang" scope="session"/>
</c:if>

<c:if test="${param.lang!=null}">
  <fmt:setLocale value="${param.lang}"/>
  <fmt:setBundle basename="com.heaton.bundles.Forum" var="lang" scope="session"/>
  <c:redirect url="index.jsp"/>
</c:if>

<c:if test="${pageContext.request.method=='POST'}">
  <c:if test="${param.reg!=null}">
    <c:redirect url="register.jsp" />
  </c:if>

  <sql:query var="users" dataSource="${dataSource}">select c_uid,c_type
  from t_users where c_uid = ? and c_pwd = ? 
  <sql:param value="${param.uid}" />

  <sql:param value="${param.pwd}" />
  </sql:query>

  <c:choose>
    <c:when test="${users.rowCount<1}">
      <h3 color="red"><fmt:message key="login.badpw" bundle="${lang}"/></h3>

      <sql:update var="result" dataSource="${dataSource}">update
      t_users set c_bad = c_bad + 1 where c_uid = ? 
      <sql:param value="${param.uid}" />
      </sql:update>
    </c:when>

    <c:otherwise>
      <%
        Cookie mycookie = new Cookie("login",request.getParameter("uid"));
        mycookie.setMaxAge(0x7ffffff);
        response.addCookie(mycookie);
       %>
      <c:forEach var="aUser" items="${users.rows}">
        <c:set var="userID" value="${aUser.c_uid}" scope="session" />

        <c:set var="userType" value="${aUser.c_type}"
        scope="session" />
      </c:forEach>

      <c:redirect url="welcome.jsp" />
    </c:otherwise>
  </c:choose>
</c:if>

<c:if test="<%=request.getCookies()!=null%>">
  <c:forEach var="aCookie" items="<%=request.getCookies()%>">
    <c:if test="${aCookie.name=='login'}">
      <c:set var="uid" value="${aCookie.value}" />
    </c:if>
  </c:forEach>
</c:if>

<html>
  <head>
    <title><fmt:message key="login.title" bundle="${lang}"/></title>
  </head>

  <body>
    <fmt:message key="login.title" bundle="${lang}"/><br>
    <form method="POST">
      <table border="1" cellpadding="0" cellspacing="0"
      style="border-collapse: collapse" bordercolor="#111111"
      width="49%" id="AutoNumber1">
        <tr>
          <td width="100%" colspan="2" bgcolor="#0000FF">
            <p align="center">
              <b>
                <font color="#FFFFFF" size="4">
                <fmt:message key="login.title" bundle="${lang}"/></font>
              </b>
            </p>
          </td>
        </tr>

        <tr>
          <td width="26%"><fmt:message key="login.name" bundle="${lang}"/></td>

          <td width="74%">
          <input type="text" name="uid" value="<c:out value="${uid}"/>" size="20" />

          </td>
        </tr>

        <tr>
          <td width="26%">
          <fmt:message key="word.password" bundle="${lang}"/></td>

          <td width="74%">
            <input type="password" name="pwd" size="20" />
          </td>
        </tr>

        <tr>
          <td width="100%" colspan="2">
            <p align="center">
              <input type="submit" value="<fmt:message key="button.login" bundle="${lang}"/>" name="Login" />

              <input type="submit" value="<fmt:message key="button.register" bundle="${lang}"/>" name="reg" />
            </p>
          </td>
        </tr>
      </table>

      <p>&#160;</p>
    </form>

    <p><fmt:message key="login.note" bundle="${lang}"/>
    </p>
    
    <a href="index.jsp?lang=en">English(<fmt:message key="lang.english" bundle="${lang}"/>)</a><br>
    <a href="index.jsp?lang=es">Espanol(<fmt:message key="lang.spanish" bundle="${lang}"/>)</a><br>
    <a href="index.jsp?lang=zh">Chinese(<fmt:message key="lang.chinese" bundle="${lang}"/>)</a><br>    
  </body>
</html>

